﻿#include "additem.h"
#include "ui_additem.h"

AddItem::AddItem(QWidget *parent) :
    BaseEditorDialog(parent),
    ui(new Ui::AddItem)
{
    ui->setupUi(this);

    table=ST_UNITS;
    model=new QSqlTableModel(this);
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    ui->tableView->setModel(model);
    ui->tableView->setEditTriggers(QTableView::DoubleClicked);

    updateTarget();
    updateCategory();
    createMenu();
}

AddItem::~AddItem()
{
    delete ui;
}

void AddItem::generateMenu()
{
    menu->addAction(QStringLiteral("撤销"),this,&AddItem::revert);
    menu->addAction(QStringLiteral("保存"),this,&AddItem::save);
    menu->addSeparator();
    menu->addAction(QStringLiteral("删除"),this,&AddItem::removeItem);
}

void AddItem::removeItem()
{
    QModelIndex index=ui->tableView->currentIndex();
    if(index.isValid())
    {
        model->removeRow(index.row());
    }
}

void AddItem::updateTarget()
{
    if(ui->units->count() != 0)
        ui->units->clear();

    //理论目标
    QStringList sources=sp_getItems("label",ST_UNIT_COST);
    if(!sources.isEmpty())
    {
        // 已存在目标
        QStringList items=sp_getCategory("label",ST_UNIT_PARTS);
        if(!items.isEmpty())
        {
            foreach (QString item, items) {
                if(sources.contains(item))
                    sources.removeAll(item);
            }
        }

        if(!sources.isEmpty())
        {
            ui->units->addItems(sources);
            ui->units->setCurrentIndex(-1);
        }
    }
}

void AddItem::updateCategory()
{
    if(ui->category->count() != 0)
        ui->category->clear();

    QStringList items=sp_getCategory("category",table);
    if(!items.isEmpty())
    {
        ui->category->addItems(items);
        ui->category->setCurrentIndex(-1);
    }
}

void AddItem::on_category_currentTextChanged(const QString &text)
{
    updateParts(text);
}

void AddItem::updateParts(const QString &category)
{
    if(ui->labels->count() != 0)
        ui->labels->clear();

    QStringList items=sp_getItems("label",table,"category",category);
    if(!items.isEmpty())
        ui->labels->addItems(items);
}

void AddItem::updateView(const QString &target)
{
    model->setTable(ST_UNIT_PARTS);
    model->setFilter(QString("label='%1'").arg(target));
    model->select();

    ui->tableView->hideColumn(0);   //id
    ui->tableView->hideColumn(1);   //label
    model->setHeaderData(2,Qt::Horizontal,QStringLiteral("配件名称"));  //name
    ui->tableView->setColumnWidth(2,240);
    model->setHeaderData(3,Qt::Horizontal,QStringLiteral("配件数量"));  //number
    model->setHeaderData(4,Qt::Horizontal,QStringLiteral("附加说明"));  //remark
}

bool AddItem::addItem(const QString &label, const QString &name, int number, const QString &remark)
{
    QSqlQuery query;
    query.prepare(QString("insert into %1 (label,name,number,remark) values (?,?,?,?)")
                  .arg(ST_UNIT_PARTS));
    query.addBindValue(label);
    query.addBindValue(name);
    query.addBindValue(number);
    query.addBindValue(remark);
    if(query.exec())
        return true;
    else
    {
        qDebug() << query.lastError().text();
        return false;
    }
}

void AddItem::on_btn_submit_clicked()
{
    QString label=ui->units->currentText();
    if(label.isEmpty())
        return;

    QString name=ui->labels->currentText();
    QString numStr=ui->number->text().trimmed();
    if(name.isEmpty() || numStr.isEmpty())
        return;

    bool ok;
    int number=numStr.toInt(&ok);
    if(!ok)
    {
        QMessageBox::warning(this,QStringLiteral("异常情况"),
                             QStringLiteral("无效输入！"));
        return;
    }

    QString remark=ui->remark->text().trimmed();
    if(addItem(label,name,number,remark))
    {
        if(ui->units->isEnabled())
            ui->units->setEnabled(false);
        ui->category->setCurrentIndex(-1);
        ui->labels->setCurrentIndex(-1);
        ui->number->clear();
        ui->remark->clear();
        updateView(label);
    }
    else
        QMessageBox::warning(this,QStringLiteral("异常情况"),
                             QStringLiteral("无法提交！"));
}
